home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / ctlfont.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  7.2 KB  |  329 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFXCTL_CORE2_SEG
  14. #pragma code_seg(AFXCTL_CORE2_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CFontHolder
  26.  
  27. CFontHolder::CFontHolder(LPPROPERTYNOTIFYSINK pNotify) :
  28.     m_pFont(NULL),
  29. WCE_INS m_pSavedFont(NULL),
  30.     m_dwConnectCookie(0),
  31.     m_pNotify(pNotify)
  32. {
  33.     ASSERT_NULL_OR_POINTER(pNotify, IPropertyNotifySink);
  34. }
  35.  
  36. void CFontHolder::SetFontNotifySink(LPPROPERTYNOTIFYSINK pNotify)
  37. {
  38.     ASSERT_NULL_OR_POINTER(pNotify, IPropertyNotifySink);
  39.     m_pNotify = pNotify;
  40. }
  41.  
  42. CFontHolder::~CFontHolder()
  43. {
  44.     ReleaseFont();
  45. }
  46.  
  47. void CFontHolder::ReleaseFont()
  48. {
  49.     if ((m_pFont != NULL) && (m_pNotify != NULL))
  50.     {
  51.         AfxConnectionUnadvise(m_pFont, IID_IPropertyNotifySink, m_pNotify,
  52.             FALSE, m_dwConnectCookie);
  53.     }
  54.  
  55.     RELEASE(m_pFont);
  56. }
  57.  
  58. AFX_STATIC_DATA const FONTDESC _afxFontDescDefault =
  59.     { sizeof(FONTDESC), WCE_IF(OLESTR("Tahoma"),OLESTR("MS Sans Serif")), WCE_IF(FONTSIZE(8),FONTSIZE(12)), FW_NORMAL,
  60.       DEFAULT_CHARSET, FALSE, FALSE, FALSE };
  61.  
  62. void CFontHolder::InitializeFont(const FONTDESC* pFontDesc,
  63.     LPDISPATCH pFontDispAmbient)
  64. {
  65.     ASSERT_NULL_OR_POINTER(pFontDesc, FONTDESC);
  66.     ASSERT_NULL_OR_POINTER(pFontDispAmbient, IDispatch);
  67. #ifdef _DEBUG
  68.     if (pFontDesc != NULL)
  69.         ASSERT(pFontDesc->cbSizeofstruct == sizeof(FONTDESC));
  70. #endif
  71.  
  72.     // Release any previous font, in preparation for creating a new one.
  73.     ReleaseFont();
  74.  
  75.     LPFONT pFontAmbient;
  76.     LPFONT pFontNew = NULL;
  77.  
  78.     if ((pFontDispAmbient != NULL) &&
  79.         SUCCEEDED(pFontDispAmbient->QueryInterface(IID_IFont,
  80.                 (LPVOID*)&pFontAmbient)))
  81.     {
  82.         ASSERT_POINTER(pFontAmbient, IFont);
  83.  
  84.         // Make a clone of the ambient font.
  85.         pFontAmbient->Clone(&pFontNew);
  86.         pFontAmbient->Release();
  87.     }
  88.     else
  89.     {
  90.         // Create the font.
  91.         if (pFontDesc == NULL)
  92.             pFontDesc = &_afxFontDescDefault;
  93.  
  94.         if (FAILED(::WCE_FCTN(OleCreateFontIndirect)((LPFONTDESC)pFontDesc, IID_IFont,
  95.                 (LPVOID *)&pFontNew)))
  96.             pFontNew = NULL;
  97.     }
  98.  
  99.     // Setup advisory connection and find dispatch interface.
  100.     if (pFontNew != NULL)
  101.         SetFont(pFontNew);
  102. }
  103.  
  104. BOOL AFXAPI _AfxIsSameFont(CFontHolder& font, const FONTDESC* pFontDesc,
  105.     LPFONTDISP pFontDispAmbient)
  106. {
  107.     if (font.m_pFont == NULL)
  108.         return FALSE;
  109.  
  110.     BOOL bSame = FALSE;
  111.  
  112.     if (pFontDispAmbient != NULL)
  113.     {
  114.         LPFONT pFontAmbient;
  115.         if (SUCCEEDED(pFontDispAmbient->QueryInterface(IID_IFont,
  116.             (LPVOID*)&pFontAmbient)))
  117.         {
  118.             ASSERT_POINTER(pFontAmbient, IFont);
  119.             bSame = pFontAmbient->IsEqual(font.m_pFont) == S_OK;
  120.             pFontAmbient->Release();
  121.         }
  122.     }
  123.     else
  124.     {
  125.         if (pFontDesc == NULL)
  126.             pFontDesc = &_afxFontDescDefault;
  127.  
  128.         bSame = TRUE;
  129.         BOOL bFlag;
  130.  
  131.         font.m_pFont->get_Italic(&bFlag);
  132.         bSame = (bFlag == pFontDesc->fItalic);
  133.  
  134.         if (bSame)
  135.         {
  136.             font.m_pFont->get_Underline(&bFlag);
  137.             bSame = (bFlag == pFontDesc->fUnderline);
  138.         }
  139.  
  140.         if (bSame)
  141.         {
  142.             font.m_pFont->get_Strikethrough(&bFlag);
  143.             bSame = (bFlag == pFontDesc->fStrikethrough);
  144.         }
  145.  
  146.         if (bSame)
  147.         {
  148.             short sCharset;
  149.             font.m_pFont->get_Charset(&sCharset);
  150.             bSame = (sCharset == pFontDesc->sCharset);
  151.         }
  152.  
  153.         if (bSame)
  154.         {
  155.             short sWeight;
  156.             font.m_pFont->get_Weight(&sWeight);
  157.             bSame = (sWeight == pFontDesc->sWeight);
  158.         }
  159.  
  160.         if (bSame)
  161.         {
  162.             CURRENCY cy;
  163.             font.m_pFont->get_Size(&cy);
  164.             bSame = (memcmp(&cy, &pFontDesc->cySize, sizeof(CURRENCY)) == 0);
  165.         }
  166.  
  167.         if (bSame)
  168.         {
  169.             BSTR bstrName;
  170.             font.m_pFont->get_Name(&bstrName);
  171.             CString strName1(bstrName);
  172.             CString strName2(pFontDesc->lpstrName);
  173.             bSame = (strName1 == strName2);
  174.             SysFreeString(bstrName);
  175.         }
  176.     }
  177.  
  178.     return bSame;
  179. }
  180.  
  181. HFONT CFontHolder::GetFontHandle()
  182. {
  183.     // Assume a screen DC for logical/himetric ratio.
  184.     return GetFontHandle(afxData.cyPixelsPerInch, HIMETRIC_PER_INCH);
  185. }
  186.  
  187. HFONT CFontHolder::GetFontHandle(long cyLogical, long cyHimetric)
  188. {
  189.     HFONT hFont = NULL;
  190.  
  191.     if ((m_pFont != NULL) &&
  192.         SUCCEEDED(m_pFont->SetRatio(cyLogical, cyHimetric)) &&
  193.         SUCCEEDED(m_pFont->get_hFont(&hFont)))
  194.     {
  195.         ASSERT(hFont != NULL);
  196.     }
  197.  
  198.     return hFont;
  199. }
  200.  
  201. CFont* CFontHolder::Select(CDC* pDC, long cyLogical, long cyHimetric)
  202. {
  203.     ASSERT_POINTER(pDC, CDC);
  204.  
  205.     HFONT hFont = NULL;
  206.  
  207.     if (m_pFont != NULL)
  208.         hFont = GetFontHandle(cyLogical, cyHimetric);
  209.  
  210.     if (hFont != NULL)
  211.     {
  212.         if ((pDC->m_hAttribDC != pDC->m_hDC) &&
  213.             (pDC->m_hAttribDC != NULL))
  214.         {
  215.             ::SelectObject(pDC->m_hAttribDC, hFont);
  216.         }
  217.  
  218.         return CFont::FromHandle((HFONT)::SelectObject(pDC->m_hDC, hFont));
  219.     }
  220.  
  221.     return NULL;
  222. }
  223.  
  224. void CFontHolder::QueryTextMetrics(LPTEXTMETRIC lptm)
  225. {
  226.     ASSERT(lptm != NULL);
  227.  
  228.     if (m_pFont != NULL)
  229.     {
  230. #if defined(_UNICODE) || defined(OLE2ANSI)
  231.         // no conversion necessary
  232.         m_pFont->QueryTextMetrics(lptm);
  233. #else
  234.         TEXTMETRICW tmw;
  235.         m_pFont->QueryTextMetrics(&tmw);
  236.         AfxTextMetricW2A(lptm, &tmw);
  237. #endif
  238.     }
  239.     else
  240.     {
  241.         memset(lptm, 0, sizeof(TEXTMETRIC));
  242.     }
  243. }
  244.  
  245. LPFONTDISP CFontHolder::GetFontDispatch()
  246. {
  247.     LPFONTDISP pFontDisp = NULL;
  248.  
  249.     if ((m_pFont != NULL) &&
  250.         SUCCEEDED(m_pFont->QueryInterface(IID_IFontDisp, (LPVOID*)&pFontDisp)))
  251.     {
  252.         ASSERT_POINTER(pFontDisp, IFontDisp);
  253.     }
  254.  
  255.     return pFontDisp;
  256. }
  257.  
  258. void CFontHolder::SetFont(LPFONT pFontNew)
  259. {
  260.     ASSERT_NULL_OR_POINTER(pFontNew, IFont);
  261.  
  262.     if (m_pFont != NULL)
  263.         ReleaseFont();
  264.  
  265.     m_pFont = pFontNew;
  266.  
  267.     if (m_pNotify != NULL)
  268.     {
  269.         AfxConnectionAdvise(m_pFont, IID_IPropertyNotifySink, m_pNotify,
  270.             FALSE, &m_dwConnectCookie);
  271.     }
  272. }
  273.  
  274. BOOL CFontHolder::GetDisplayString(CString& strValue)
  275. {
  276.     return strValue.LoadString(AFX_IDS_DISPLAYSTRING_FONT);
  277. }
  278.  
  279. #if defined(_WIN32_WCE)
  280. void CFontHolder::FontToCeFont()
  281. {
  282.     // WinCE: we're not assuming that m_pFont is *not* a CCeFont*
  283.     FONTDESC fd;
  284.     fd.cbSizeofstruct = sizeof(FONTDESC);
  285.  
  286.     if(m_pFont == NULL)
  287.         return;
  288.  
  289.     BSTR bstrName = NULL;
  290.     if(SUCCEEDED(m_pFont->get_Name(&bstrName)))
  291.         fd.lpstrName = bstrName;
  292.     else
  293.         return;
  294.  
  295.     m_pFont->get_Size(&fd.cySize);
  296.     m_pFont->get_Weight(&fd.sWeight);
  297.     m_pFont->get_Charset(&fd.sCharset);
  298.     m_pFont->get_Italic(&fd.fItalic);
  299.     m_pFont->get_Underline(&fd.fUnderline);
  300.     m_pFont->get_Strikethrough(&fd.fStrikethrough);
  301.  
  302.     IFont *pFontNew;
  303.     if(SUCCEEDED(::WCE_FCTN(OleCreateFontIndirect)(&fd, IID_IFont, (LPVOID *)&pFontNew)))
  304.     {
  305.         m_pSavedFont = m_pFont;
  306.         m_pFont = pFontNew;
  307.     }
  308.  
  309.     if(bstrName != NULL)
  310.         SysFreeString(bstrName);
  311. }
  312.  
  313. void CFontHolder::RestoreFont()
  314. {
  315.     if(m_pSavedFont != NULL)
  316.     {
  317.         m_pFont->Release();
  318.         m_pFont = m_pSavedFont;
  319.         m_pSavedFont = NULL;
  320.     }
  321. }
  322. #endif // _WIN32_WCE
  323. /////////////////////////////////////////////////////////////////////////////
  324. // Force any extra compiler-generated code into AFX_INIT_SEG
  325.  
  326. #ifdef AFX_INIT_SEG
  327. #pragma code_seg(AFX_INIT_SEG)
  328. #endif
  329.